﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using GroupCheer.FBGraphAPI;

namespace GroupCheer.FBGraphAPI.Core
{
    public class Network
    {
        public static string MakeHttpPost(string RequestUrl)
        {
            return MakeHttpPost(RequestUrl, new Dictionary<string, string>());
        }

        public static string MakeHttpPost(string RequestUrl, Dictionary<string, string> HttpPostData)
        {
            HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
            HttpWebResponse response = null;

            StreamReader sr = null;
            StreamWriter sw = null;

            try
            {
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                StringBuilder sb = new StringBuilder();

                sb.Append("access_token=" + FBGraphAPIConfiguration.FBValidAccessToken);

                if (HttpPostData != null && HttpPostData.Count > 0)
                {
                    foreach (KeyValuePair<string, string> postItem in HttpPostData)
                    {
                        if (sb.Length == 0)
                            sb.Append(string.Format("{0}={1}", postItem.Key, postItem.Value));
                        else
                            sb.Append(string.Format("&{0}={1}", postItem.Key, postItem.Value));
                    }
                }

                sw = new StreamWriter(request.GetRequestStream());
                sw.Write(sb.ToString());
                sw.Close();

                response = request.GetResponse() as HttpWebResponse;

                sr = new StreamReader(response.GetResponseStream());
                string responseData = sr.ReadToEnd();
                sr.Close();


                response.Close();

                return responseData;
            }
            catch (WebException we)
            {
                throw new FBGraphAPIException((we.Response as HttpWebResponse));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string MakeHttpPost(string RequestUrl, Dictionary<string, string> HttpPostData, PostFileData FileUploadData)
        {
            HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
            HttpWebResponse response = null;

            StreamReader sr = null;
            string boundary = "FBGraphAPIClient" + DateTime.Now.Ticks.ToString();

            try
            {
                request.Method = "POST";
                request.ContentType = "multipart/form-data; boundary=----" + boundary;

                byte[] multipartPostData = Helpers.BuildMultipartPostData(boundary, HttpPostData, FileUploadData);

                BinaryWriter bw = new BinaryWriter(request.GetRequestStream());
                bw.Write(multipartPostData);
                bw.Close();

                response = request.GetResponse() as HttpWebResponse;

                sr = new StreamReader(response.GetResponseStream());
                string responseData = sr.ReadToEnd();
                sr.Close();


                response.Close();

                return responseData;
            }
            catch (WebException we)
            {
                throw new FBGraphAPIException((we.Response as HttpWebResponse));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string MakeHttpGet(string RequestUrl)
        {
            HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
            HttpWebResponse response = null;

            StreamReader sr = null;

            try
            {
                request.Method = "GET";

                response = request.GetResponse() as HttpWebResponse;

                sr = new StreamReader(response.GetResponseStream());
                string responseData = sr.ReadToEnd();
                sr.Close();

                response.Close();

                return responseData;
            }
            catch (WebException we)
            {
                throw new FBGraphAPIException((we.Response as HttpWebResponse));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string MakeHttpDelete(string RequestUrl)
        {
            HttpWebRequest request = WebRequest.Create(RequestUrl) as HttpWebRequest;
            HttpWebResponse response = null;

            StreamReader sr = null;

            try
            {
                request.Method = "DELETE";

                response = request.GetResponse() as HttpWebResponse;

                sr = new StreamReader(response.GetResponseStream());
                string responseData = sr.ReadToEnd();
                sr.Close();

                response.Close();

                return responseData;
            }
            catch (WebException we)
            {
                throw new FBGraphAPIException((we.Response as HttpWebResponse));
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
